﻿<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<%

  String[] dictionary = {
    "a",
    "abc",
    "bad",
    "bear",
    "beer",
    "book",
    "boy",
    "c",
    "can",
    "case",
    "car",
    "dad",
    "daemon",
    "deaf",
    "deed",
    "dig",
    "dog",
    "doom",
    "e",
    "each",
    "email",
    "face",
    "facebook",
    "fans",
    "far",
    "farther",
    "father",
    "x",
    "xyz",
    "xiaonei"
  };

	String fortext = request.getParameter("suggest_typed");
	if (fortext == null) {
		fortext = "";
	}

	StringBuffer value = new StringBuffer(128);
  value.append("{\"fortext\": \"");
  value.append(fortext);
  value.append("\", \"results\": [");

  if (!fortext.equals("")) {
    for (int i = 0; i < dictionary.length; i++) {
      if (dictionary[i].charAt(0) > fortext.charAt(0)) { //no more match
        break;
      }
      else if (dictionary[i].indexOf(fortext) == 0) {
        value.append("\"");
        value.append(dictionary[i]);
        value.append("\",");
      }
    }
    
		if (value.length() > 0 && value.charAt(value.length() - 1) == ',') {
			value.deleteCharAt(value.length() - 1);
		}
  }

  value.append("]}");
  out.println(value);
%>
